#include "MessageCenter.h"
#include "Util.h"
#include "Encryptor.h"
#include "string.h"
MessageCenter::MessageCenter(UserLogon* user)
{
	userLogon = user;
	InitializeCriticalSection(&csLock);
	hNotEmpty = 1;
	messageQueue = new STRINGQUEUE();

	int serverId = -1;
	int mobileId = -1;
	int socketId = -1;
	sender = NULL;
}

MessageCenter::~MessageCenter(void)
{
}

void MessageCenter::addClientSender(ClientSender* sender) {
	this->sender = sender;
}
void MessageCenter::enqueueMessage(char *message)
{
	EnterCriticalSection(&csLock);
	messageQueue->push(message);
	LeaveCriticalSection(&csLock);
	SetEvent(&hNotEmpty);
}
void MessageCenter::start() 
{
	DWORD dwThreadId;
	if(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&handleMessage, (LPVOID) this, 0, &dwThreadId) == NULL)
	{
		return;
	}
	
}
void MessageCenter::sendMessage(char *message, int desId) {
	if (this->sender != NULL) {
		
		XmlHandler* handler = XmlHandler::getMessageHandler(message);
		if (handler == NULL) {
			return;
		}
		handler->sender = socketId;
		handler->receiver = desId;
		sender->sendMessage(handler->toXmlString());		
	}
}

void MessageCenter::handleResponseMessage(XmlResponseMessageHandler* xml) {

}
void MessageCenter::handleIssuerMessage(XmlIssuerMessageHandler* xml) {
	this->mobileId = xml->mobileId;
	this->serverId = xml->windowsServiceId;
	this->socketId = xml->clientId;
}

void simulate_ENTER_Key_press()
{

	keybd_event( VK_TAB, 0x45, 0, 0 );

    // Simulate a key press

    keybd_event( VK_RETURN, 0x45, 0, 0 );

    // Simulate a key release

    keybd_event( VK_RETURN,    0x45, KEYEVENTF_KEYUP, 0);


} 

void MessageCenter::handleMessage(MessageCenter* mCenter) {	
	while (1) 
	{
		HANDLE handle = &(mCenter->hNotEmpty);
		EnterCriticalSection(&(mCenter->csLock));
		if (mCenter->messageQueue->empty()) {			
			LeaveCriticalSection(&(mCenter->csLock));
			WaitForSingleObject(handle, INFINITE);
		}
		else {
			char* message = mCenter->messageQueue->front();
			mCenter->messageQueue->pop();
			LeaveCriticalSection(&(mCenter->csLock));						

			if(message != NULL){
				Util::log(message);
				
				if(message[0]=='$'){					

					WCHAR* res[10];
					WCHAR * pch;
					int i  = 0;
					int j = 0;
					//printf ("Splitting string \"%s\" into tokens:\n",str);
					DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, message, -1, NULL, 0);
					wchar_t *message2;
					message2 = new wchar_t[dwNum];										
					MultiByteToWideChar (CP_ACP, 0, message, -1, message2, dwNum );
										
					pch = wcstok (message2,L"$");
					while (pch != NULL)
					{
						res[i++] = pch;
						//printf ("%s\n",pch);
						pch = wcstok (NULL, L"$");
					}
					if(!message2)
					{
						delete []message2;
					}

					Encryptor* encryptor = new Encryptor();
					// @khangvn: FOR TEST					
					// END FOR TEST										

					mCenter->userLogon->userName = encryptor->decrypt(res[2],mCenter->userLogon->key);
					mCenter->userLogon->password = encryptor->decrypt(res[3],mCenter->userLogon->key);
					
					//MessageBox(0,wcscat(L"You just enter account: ",res[1]),L"Information",0);										
					simulate_ENTER_Key_press();
					MessageBox(0,mCenter->userLogon->userName,_T("You login as"),0);

					delete encryptor;					
				}
			}

			
			XmlHandler* xml = XmlHandler::getMessageHandler(message);
			if (xml == NULL) return;
			int type = xml->type;
						/*
			switch (type)
			{
				case Constant::EncryptedMessage:
					break;
				case Constant::AddMessage:
					break;
				case Constant::EditMessage:
					break;
				case Constant::DeleteMessage:
					break;
				case Constant::ResponseMessage:
					Util::log("resopnese");
					mCenter->handleResponseMessage((XmlResponseMessageHandler*) xml);
					break;
				case Constant::RequestMessage:
					Util::log("request");
					break;
				case Constant::IssuerMessage:
					Util::log("issuer");
					mCenter->handleIssuerMessage((XmlIssuerMessageHandler*) xml);
					break;
			                
			}
			*/

			
		}
	}
}